package com.google.code.mochaccino.framework.xml.stax;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.code.mochaccino.framework.xml.TranscodingUtil;
import java.util.Collection;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.events.Attribute;
import org.xml.sax.Attributes;

/** Implementation of the StAXWriter Interface. */
public class StAXWriterImpl extends StAXWriterBase {

	/** Constructor */
	public StAXWriterImpl( XMLStreamWriter xmlWriter ) {
		super( xmlWriter );
	}

	@Override
	public void writeAttribute( Collection<Attribute> attr ) throws XMLStreamException {
		if ( attr == null ) {
			return;
		}
		for ( Object wElement : attr ) {
			Attribute wAttr = (Attribute) wElement;
			writeAttribute( wAttr );
		}
	}

	@Override
	public void writeAttribute( Attribute attr ) throws XMLStreamException {
		if ( attr == null ) {
			return;
		}
		QName wQName = attr.getName();
		writer.writeAttribute( wQName.getPrefix(), wQName.getNamespaceURI(), wQName.getLocalPart(), attr.getValue() );
	}

	@Override
	public void writeAttribute( org.xml.sax.Attributes attr ) throws XMLStreamException {
		if ( attr == null ) {
			return;
		}
		for ( int I = 0; I < attr.getLength(); I++ ) {
			writer.writeAttribute( attr.getLocalName( I ), attr.getValue( I ) );
		}
	}

	@Override
	public <T> void writeAttribute( String prefix, String ns, String name, T value ) throws XMLStreamException {
		if ( value == null ) {
			return;
		}
		writer.writeAttribute( prefix, ns, name, TranscodingUtil.encode( value ) );
	}

	@Override
	public <T> void writeAttribute( String ns, String name, T value ) throws XMLStreamException {
		if ( value == null ) {
			return;
		}
		writer.writeAttribute( ns, name, TranscodingUtil.encode( value ) );
	}

	@Override
	public <T> void writeAttribute( String name, T value ) throws XMLStreamException {
		if ( value == null ) {
			return;
		}
		writer.writeAttribute( name, TranscodingUtil.encode( value ) );
	}

	@Override
	public <T> void writeCharacters( T value ) throws XMLStreamException {
		if ( value != null ) {
			writer.writeCharacters( TranscodingUtil.encode( value ) );
		}
	}

	@Override
	public void writeElement( String name, String value ) throws XMLStreamException {
		writeStartElement( name );
		if ( value != null ) {
			writeCharacters( value );
		}
		writeEndElement();
	}

	@Override
	public void writeElement( String name, String value, Attributes attr ) throws XMLStreamException {
		writeStartElement( name );
		writeAttribute( attr );
		if ( value != null ) {
			writeCharacters( value );
		}
		writeEndElement();
	}

	@Override
	public void writeElement( String name, String value, Collection<Attribute> attr ) throws XMLStreamException {
		writeStartElement( name );
		writeAttribute( attr );
		writeCharacters( value );
		writeEndElement();
	}

	@Override
	public void writeElement( String ns, String name, String value ) throws XMLStreamException {
		writeStartElement( ns, name );
		writeCharacters( value );
		writeEndElement();
	}

	@Override
	public void writeElement( String ns, String name, String value, Attributes attr ) throws XMLStreamException {
		writeStartElement( ns, name );
		writeAttribute( attr );
		writeCharacters( value );
		writeEndElement();
	}

	@Override
	public void writeElement( String ns, String name, String value, Collection<Attribute> attr ) throws XMLStreamException {
		writeStartElement( ns, name );
		writeAttribute( attr );
		writeCharacters( value );
		writeEndElement();
	}

	@Override
	public void writeElement( String prefix, String ns, String name, String value ) throws XMLStreamException {
		writeStartElement( prefix, ns, name );
		writeCharacters( value );
		writeEndElement();
	}

	@Override
	public void writeElement( String prefix, String ns, String name, String value, Attributes attr ) throws XMLStreamException {
		writeStartElement( prefix, ns, name );
		writeAttribute( attr );
		writeCharacters( value );
		writeEndElement();
	}

	@Override
	public void writeElement( String prefix, String ns, String name, String value, Collection<Attribute> attr ) throws XMLStreamException {
		writeStartElement( prefix, ns, name );
		writeAttribute( attr );
		writeCharacters( value );
		writeEndElement();
	}

	@Override
	public <T> void writeElement( String prefix, String ns, String name, T value ) throws XMLStreamException {
		writeStartElement( prefix, ns, name );
		writeCharacters( value );
		writeEndElement();
	}

	@Override
	public <T> void writeElement( String prefix, String ns, String name, T value, Attributes attr ) throws XMLStreamException {
		writeStartElement( prefix, ns, name );
		writeAttribute( attr );
		writeCharacters( value );
		writeEndElement();
	}

	@Override
	public <T> void writeElement( String prefix, String ns, String name, T value, Collection<Attribute> attr ) throws XMLStreamException {
		writeStartElement( prefix, ns, name );
		writeAttribute( attr );
		writeCharacters( value );
		writeEndElement();
	}

	@Override
	public <T> void writeElement( String ns, String name, T value ) throws XMLStreamException {
		writeStartElement( ns, name );
		writeCharacters( value );
		writeEndElement();
	}

	@Override
	public <T> void writeElement( String ns, String name, T value, Attributes attr ) throws XMLStreamException {
		writeStartElement( ns, name );
		writeAttribute( attr );
		writeCharacters( value );
		writeEndElement();
	}

	@Override
	public <T> void writeElement( String ns, String name, T value, Collection<Attribute> attr ) throws XMLStreamException {
		writeStartElement( ns, name );
		writeAttribute( attr );
		writeCharacters( value );
		writeEndElement();
	}

	@Override
	public <T> void writeElement( String name, T value ) throws XMLStreamException {
		writeStartElement( name );
		writeCharacters( value );
		writeEndElement();
	}

	@Override
	public <T> void writeElement( String name, T value, Attributes attr ) throws XMLStreamException {
		writeStartElement( name );
		writeAttribute( attr );
		writeCharacters( value );
		writeEndElement();
	}

	@Override
	public <T> void writeElement( String name, T value, Collection<Attribute> attr ) throws XMLStreamException {
		writeStartElement( name );
		writeAttribute( attr );
		writeCharacters( value );
		writeEndElement();
	}

	@Override
	public void writeEmptyElement( String name, Attributes attr ) throws XMLStreamException {
		writeStartElement( name );
		writeAttribute( attr );
		writeEndElement();
	}

	@Override
	public void writeEmptyElement( String name, Collection<Attribute> attr ) throws XMLStreamException {
		writeStartElement( name );
		writeAttribute( attr );
		writeEndElement();
	}

	@Override
	public void writeEmptyElement( String ns, String name, Attributes attr ) throws XMLStreamException {
		writeStartElement( ns, name );
		writeAttribute( attr );
		writeEndElement();
	}

	@Override
	public void writeEmptyElement( String ns, String name, Collection<Attribute> attr ) throws XMLStreamException {
		writeStartElement( ns, name );
		writeAttribute( attr );
		writeEndElement();
	}

	@Override
	public void writeEmptyElement( String prefix, String name, String ns, Attributes attr ) throws XMLStreamException {
		writeStartElement( prefix, ns, name );
		writeAttribute( attr );
		writeEndElement();
	}

	@Override
	public void writeEmptyElement( String prefix, String name, String ns, Collection<Attribute> attr ) throws XMLStreamException {
		writeStartElement( prefix, ns, name );
		writeAttribute( attr );
		writeEndElement();
	}
}
